/*     */ package com.zimbra.cs.mime;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.DefaultContentHandler;
/*     */ import com.zimbra.common.calendar.ZCalendar.ICalTok;
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.mime.ContentType;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.common.zmime.ZMimeBodyPart;
/*     */ import com.zimbra.common.zmime.ZMimeMultipart;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.cs.util.tnef.DefaultTnefToICalendar;
/*     */ import com.zimbra.cs.util.tnef.TnefToICalendar;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.StringWriter;
/*     */ import java.io.Writer;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import javax.mail.BodyPart;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeBodyPart;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ import net.freeutils.tnef.TNEFInputStream;
/*     */ import net.freeutils.tnef.TNEFUtils;
/*     */ import net.freeutils.tnef.mime.TNEFMime;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TnefConverter
/*     */   extends MimeVisitor
/*     */ {
/*     */   private MimeMessage mMimeMessage;
/*     */   
/*     */   protected boolean visitBodyPart(MimeBodyPart bp)
/*     */   {
/*  86 */     return false;
/*     */   }
/*     */   
/*     */   protected boolean visitMessage(MimeMessage msg, MimeVisitor.VisitPhase visitKind) throws MessagingException
/*     */   {
/*  91 */     if (visitKind == MimeVisitor.VisitPhase.VISIT_BEGIN) {
/*  92 */       this.mMimeMessage = msg;
/*  93 */       return false;
/*     */     }
/*     */     
/*     */     try
/*     */     {
/*  98 */       if (!TNEFUtils.isTNEFMimeType(msg.getContentType())) {
/*  99 */         return false;
/*     */       }
/*     */       
/* 102 */       if ((this.mCallback != null) && (!this.mCallback.onModification()))
/* 103 */         return false;
/*     */     } catch (MessagingException e) {
/* 105 */       ZimbraLog.extensions.warn("exception while checking message for TNEF content; skipping part");
/* 106 */       return false;
/*     */     }
/*     */     
/* 109 */     MimeMultipart multi = null;
/* 110 */     MimeBodyPart tnefPart = null;
/*     */     try {
/* 112 */       Object content = msg.getContent();
/* 113 */       if (!(content instanceof MimeBodyPart))
/* 114 */         return false;
/* 115 */       tnefPart = (MimeBodyPart)content;
/*     */       
/*     */ 
/* 118 */       multi = expandTNEF(tnefPart);
/* 119 */       if (multi == null) {
/* 120 */         return false;
/*     */       }
/*     */       
/* 123 */       multi.addBodyPart(tnefPart, 0);
/*     */     } catch (MessagingException e) {
/* 125 */       ZimbraLog.extensions.warn("exception while decoding TNEF message content; skipping part", e);
/* 126 */       return false;
/*     */     } catch (IOException e) {
/* 128 */       ZimbraLog.extensions.warn("exception while decoding TNEF message content; skipping part", e);
/* 129 */       return false;
/*     */     }
/*     */     
/*     */ 
/* 133 */     msg.setContent(multi);
/* 134 */     msg.setHeader("Content-Type", multi.getContentType() + "; generated=true");
/* 135 */     return true;
/*     */   }
/*     */   
/*     */   private static class MultipartReplacement {
/*     */     int partIndex;
/*     */     MimeBodyPart tnefPart;
/*     */     MimeMultipart converted;
/*     */     
/*     */     MultipartReplacement(int idx, MimeBodyPart tnef, MimeMultipart multi) {
/* 144 */       this.partIndex = idx;this.tnefPart = tnef;this.converted = multi;
/*     */     }
/*     */   }
/*     */   
/*     */   protected boolean visitMultipart(MimeMultipart mmp, MimeVisitor.VisitPhase visitKind)
/*     */     throws MessagingException
/*     */   {
/* 151 */     if (visitKind != MimeVisitor.VisitPhase.VISIT_END) {
/* 152 */       return false;
/*     */     }
/* 154 */     if ("multipart/alternative".equals(mmp.getContentType())) {
/* 155 */       return false;
/*     */     }
/*     */     try {
/* 158 */       boolean found = false;
/* 159 */       for (int i = 0; (i < mmp.getCount()) && (!found); i++) {
/* 160 */         BodyPart bp = mmp.getBodyPart(i);
/* 161 */         found = ((bp instanceof MimeBodyPart)) && (TNEFUtils.isTNEFMimeType(bp.getContentType()));
/*     */       }
/* 163 */       if (!found) {
/* 164 */         return false;
/*     */       }
/*     */       
/* 167 */       if ((this.mCallback != null) && (!this.mCallback.onModification()))
/* 168 */         return false;
/*     */     } catch (MessagingException e) {
/* 170 */       ZimbraLog.extensions.warn("exception while traversing multipart; skipping", e);
/* 171 */       return false;
/*     */     }
/*     */     
/* 174 */     List<MultipartReplacement> tnefReplacements = new ArrayList();
/*     */     try {
/* 176 */       for (int i = 0; i < mmp.getCount(); i++) {
/* 177 */         BodyPart bp = mmp.getBodyPart(i);
/* 178 */         if (((bp instanceof MimeBodyPart)) && (TNEFUtils.isTNEFMimeType(bp.getContentType())))
/*     */         {
/* 180 */           MimeMultipart multi = null;
/*     */           try {
/* 182 */             multi = expandTNEF((MimeBodyPart)bp);
/*     */           } catch (Exception e) {
/* 184 */             ZimbraLog.extensions.warn("exception while decoding TNEF; skipping part", e);
/* 185 */             continue;
/*     */           }
/* 187 */           if (multi != null)
/*     */           {
/*     */ 
/* 190 */             tnefReplacements.add(new MultipartReplacement(i, (MimeBodyPart)bp, multi)); }
/*     */         }
/*     */       }
/*     */     } catch (MessagingException e) {
/* 194 */       ZimbraLog.extensions.warn("exception while traversing multipart; skipping", e);
/* 195 */       return false;
/*     */     }
/*     */     
/* 198 */     for (MultipartReplacement change : tnefReplacements)
/*     */     {
/* 200 */       mmp.removeBodyPart(change.partIndex);
/* 201 */       change.converted.addBodyPart(change.tnefPart, 0);
/*     */       
/*     */ 
/* 204 */       MimeBodyPart replacementPart = new ZMimeBodyPart();
/* 205 */       replacementPart.setContent(change.converted);
/*     */       
/*     */ 
/* 208 */       mmp.addBodyPart(replacementPart, change.partIndex);
/*     */     }
/* 210 */     return !tnefReplacements.isEmpty();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private MimeMultipart expandTNEF(MimeBodyPart bp)
/*     */     throws MessagingException, IOException
/*     */   {
/* 220 */     if (!TNEFUtils.isTNEFMimeType(bp.getContentType())) {
/* 221 */       return null;
/*     */     }
/* 223 */     MimeMessage converted = null;
/*     */     
/*     */ 
/* 226 */     InputStream is = null;
/*     */     try {
/* 228 */       TNEFInputStream tnefis = new TNEFInputStream(is = bp.getInputStream());
/* 229 */       converted = TNEFMime.convert(JMSession.getSession(), tnefis);
/*     */ 
/*     */     }
/*     */     catch (Throwable t)
/*     */     {
/*     */ 
/* 235 */       ZimbraLog.extensions.warn("Conversion failed.  TNEF attachment will not be expanded.", t);
/* 236 */       return null;
/*     */     } finally {
/* 238 */       ByteUtil.closeStream(is);
/*     */     }
/*     */     
/* 241 */     MimeMultipart convertedMulti = (MimeMultipart)converted.getContent();
/*     */     
/* 243 */     for (int i = 0; i < convertedMulti.getCount(); i++) {
/* 244 */       BodyPart subpart = convertedMulti.getBodyPart(i);
/* 245 */       if (subpart.getHeader("Content-Disposition") == null) {
/* 246 */         subpart.setHeader("Content-Disposition", "attachment");
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 252 */     Object convertedPart = new ZMimeBodyPart();
/* 253 */     ((MimeBodyPart)convertedPart).setContent(convertedMulti);
/*     */     
/*     */ 
/* 256 */     Object icalPart = null;
/* 257 */     if (DebugConfig.enableTnefToICalendarConversion) {
/*     */       try {
/* 259 */         TnefToICalendar calConverter = new DefaultTnefToICalendar();
/* 260 */         ZCalendar.DefaultContentHandler icalHandler = new ZCalendar.DefaultContentHandler();
/* 261 */         if ((calConverter.convert(this.mMimeMessage, bp.getInputStream(), icalHandler)) && 
/* 262 */           (icalHandler.getNumCals() > 0)) {
/* 263 */           List<ZCalendar.ZVCalendar> cals = icalHandler.getCals();
/* 264 */           Writer writer = new StringWriter(1024);
/* 265 */           ZCalendar.ICalTok method = null;
/* 266 */           for (ZCalendar.ZVCalendar cal : cals) {
/* 267 */             cal.toICalendar(writer);
/* 268 */             if (method == null)
/* 269 */               method = cal.getMethod();
/*     */           }
/* 271 */           writer.close();
/* 272 */           icalPart = new ZMimeBodyPart();
/* 273 */           ((MimeBodyPart)icalPart).setText(writer.toString());
/* 274 */           ContentType ct = new ContentType("text/calendar");
/* 275 */           ct.setCharset("utf-8");
/* 276 */           if (method != null)
/* 277 */             ct.setParameter("method", method.toString());
/* 278 */           ((MimeBodyPart)icalPart).setHeader("Content-Type", ct.toString());
/*     */         }
/*     */       }
/*     */       catch (ServiceException e) {
/* 282 */         throw new MessagingException("TNEF to iCalendar conversion failure: " + e.getMessage(), e);
/*     */       }
/*     */       catch (Throwable t) {
/* 285 */         ZimbraLog.extensions.warn("Failed to convert TNEF to iCal", t);
/* 286 */         throw new MessagingException("TNEF to iCalendar conversion failure");
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 291 */     MimeMultipart altMulti = new ZMimeMultipart("alternative");
/*     */     
/* 293 */     altMulti.addBodyPart((BodyPart)convertedPart);
/* 294 */     if (icalPart != null) {
/* 295 */       altMulti.addBodyPart((BodyPart)icalPart);
/*     */     }
/* 297 */     return altMulti;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mime/TnefConverter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */